package cn.xspace.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.R;
import com.xspace.android.xafcommon.util.LogUtil;

import java.lang.reflect.Field;


/**
 * wubj 创建于 2017/2/16.
 */
public class TitleBar extends RelativeLayout {

    private static final String TAG = TitleBar.class.getSimpleName();

    protected RelativeLayout titleLayout;
    protected RelativeLayout leftLayout;
    protected ImageView leftImage;
    protected TextView leftText;
    protected LinearLayout rightLayout;
    protected ImageView rightImage;
    protected ImageView rightFilter;//右侧多的图标,一般作为筛选使用
    protected TextView rightText;
    protected LinearLayout centerLayout;
    protected TextView titleView;
    protected TextView titleDown;
    protected TextView txtNumber;
    protected ImageView titleArrow;
    protected ImageView titleArrowDown;
    protected RelativeLayout childRoot;

    public TitleBar(Context mContext, AttributeSet attrs, int defStyle) {
        this(mContext, attrs);
    }

    public TitleBar(Context mContext, AttributeSet attrs) {
        super(mContext, attrs);
        init(mContext, attrs);
    }

    public TitleBar(Context mContext) {
        super(mContext);
        init(mContext, null);
    }

    private void init(Context mContext, AttributeSet attrs) {
        LayoutInflater.from(mContext).inflate(R.layout.layout_top_titlebar, this, true);
        titleLayout = (RelativeLayout) findViewById(R.id.title_bar_root); //根部局
        leftLayout = (RelativeLayout) findViewById(R.id.layout_title_bar_left); //左侧布局
        leftImage = (ImageView) findViewById(R.id.iv_title_bar_left);
        leftText = (TextView) findViewById(R.id.tv_title_bar_left);
        rightLayout = (LinearLayout) findViewById(R.id.layout_title_bar_right);//右侧布局
        rightImage = (ImageView) findViewById(R.id.iv_title_bar_right);
        rightFilter = (ImageView) findViewById(R.id.iv_title_bar_filter);
        rightText = (TextView) findViewById(R.id.tv_title_bar_right);
        //中间标题栏
        centerLayout = (LinearLayout) findViewById(R.id.layout_title_bar_center);//中间布局
        txtNumber = (TextView) findViewById(R.id.tv_title_bar_num);
        titleView = (TextView) findViewById(R.id.tv_title_bar_title); //标题栏
        titleDown = (TextView) findViewById(R.id.tv_title_bar_down); //中间布局中下方的箭头
        titleArrow = (ImageView) findViewById(R.id.iv_title_bar_arrow_right); //上下的箭头
        titleArrowDown = (ImageView) findViewById(R.id.iv_title_bar_arrow_down); //中间布局中下方的箭头
        childRoot = (RelativeLayout) findViewById(R.id.rl_child_root);

        parseStyle(mContext, attrs);
    }

    private void parseStyle(Context mContext, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.TitleBar);
            String title = ta.getString(R.styleable.TitleBar_titleBarTitle);

            if (titleView != null) {
                titleView.setText(title);
            }
            //右侧图片默认显示
            boolean isRightImageViewVisible = ta.getBoolean(R.styleable.TitleBar_titleBarRightImageVisible, true);
            if (rightImage != null) {
                if (isRightImageViewVisible) {
                    rightImage.setVisibility(View.VISIBLE);
                } else {
                    rightImage.setVisibility(View.GONE);
                }
            }
            //左侧图片默认显示
            boolean isLeftImageViewVisible = ta.getBoolean(R.styleable.TitleBar_titleBarLeftImageVisible, true);
            if (leftImage != null) {
                if (isLeftImageViewVisible) {
                    leftImage.setVisibility(View.VISIBLE);
                } else {
                    leftImage.setVisibility(View.GONE);
                }
            }
            //右侧布局默认显示
            boolean isRightLayoutVisible = ta.getBoolean(R.styleable.TitleBar_titleBarRightLayoutVisible, true);
            if (rightLayout != null) {
                rightLayout.setVisibility(isRightLayoutVisible ? View.VISIBLE : View.GONE);
            }

            //右侧Text,和右侧图片只能显示一个,默认显示图片
            boolean isRightTextViewVisible = ta.getBoolean(R.styleable.TitleBar_titleBarRightTextVisible, false);
            if (isRightTextViewVisible) {
                rightText.setVisibility(View.VISIBLE);
                rightImage.setVisibility(View.GONE);
                String rightTitle = ta.getString(R.styleable.TitleBar_titleBarRightText);
                if (rightText != null && !TextUtils.isEmpty(rightTitle)) {
                    rightText.setText(rightTitle);
                }
            } else {
                rightText.setVisibility(View.GONE);
                rightImage.setVisibility(View.VISIBLE);
            }

            //左侧Text,右侧图片只能显示一个,默认显示图片
            boolean isLeftTextViewVisible = ta.getBoolean(R.styleable.TitleBar_titleBarLeftTextVisible, false);
            if (isLeftTextViewVisible) { //显示文字时时才设置内容
                leftText.setVisibility(View.VISIBLE);
                leftImage.setVisibility(View.GONE);
                String leftTitle = ta.getString(R.styleable.TitleBar_titleBarLeftText);
                if (leftText != null && !TextUtils.isEmpty(leftTitle)) {
                    leftText.setText(leftTitle);
                }
            } else {
                leftText.setVisibility(View.GONE);
                leftImage.setVisibility(View.VISIBLE);
            }

            Drawable leftDrawable = ta.getDrawable(R.styleable.TitleBar_titleBarLeftImage);
            if (null != leftDrawable) {
                leftImage.setImageDrawable(leftDrawable);
            }
            Drawable rightDrawable = ta.getDrawable(R.styleable.TitleBar_titleBarRightImage);
            if (null != rightDrawable) {
                rightImage.setImageDrawable(rightDrawable);
            }
            //背景
            Drawable background = ta.getDrawable(R.styleable.TitleBar_titleBarBackground);
            if (null != background) {
                titleLayout.setBackgroundDrawable(background);
            }

            ta.recycle();
        }
    }

    /**
     * 获取标题栏的箭头
     *
     * @return
     */
    public ImageView getTitleArrow() {
        return titleArrow;
    }

    /**
     * 设置箭头是否可见
     *
     * @param visibility
     */
    public void setTitleArrowVisibility(int visibility) {
        titleArrow.setVisibility(visibility);
    }

    /**
     * 设置向下箭头是否可见
     *
     * @param visibility
     */
    public void setTitleArrowDownVisibility(int visibility) {
        titleArrowDown.setVisibility(visibility);
    }

    /**
     * 设置标题栏中间下方文字的是否可见
     *
     * @param visibility
     */
    public void setTitleDownTextVisibility(int visibility) {
        titleDown.setVisibility(visibility);
    }

    /**
     * 设置下方文字内容
     *
     * @param titleDownStr
     */
    public void setTitleDownText(String titleDownStr) {
        titleDown.setText(titleDownStr);
    }

    /**
     * 右侧新增按钮一般为筛选使用,有筛选的可以直接显示使用
     *
     * @param visibility
     */
    public void setRightFilterVisibility(int visibility) {
        rightFilter.setVisibility(visibility);
    }

    public void setFilterClickListener(OnClickListener listener) {
        rightFilter.setOnClickListener(listener);
    }

    public void setFilterImageResource(int resId) {
        rightFilter.setImageResource(resId);
    }

    /**
     * 只显示左侧字体
     */
    public void showLeftText() {
        leftLayout.setVisibility(View.VISIBLE);
        leftText.setVisibility(View.VISIBLE);
        leftImage.setVisibility(View.GONE);
    }

    /**
     * 只显示左侧图片
     */
    public void showLeftImage() {
        leftLayout.setVisibility(View.VISIBLE);
        leftText.setVisibility(View.GONE);
        leftImage.setVisibility(View.VISIBLE);
    }

    /**
     * 只显示右侧问题
     */
    public void showRightText() {
        rightLayout.setVisibility(View.VISIBLE);
        rightText.setVisibility(View.VISIBLE);
        rightImage.setVisibility(View.GONE);
    }

    /**
     * 只显示右侧图片
     */
    public void showRightImage() {
        rightLayout.setVisibility(View.VISIBLE);
        leftText.setVisibility(View.GONE);
        leftImage.setVisibility(View.VISIBLE);
    }

    //左侧布局是否可见
    public void setLeftLayoutVisibility(int visibility) {
        leftLayout.setVisibility(visibility);
    }

    //右侧布局是否可见
    public void setRightLayoutVisibility(int visibility) {
        rightLayout.setVisibility(visibility);
    }

    //左侧图片
    public void setLeftImageResource(int resId) {
        leftImage.setImageResource(resId);
    }

    //右侧图片
    public void setRightImageResource(int resId) {
        rightImage.setImageResource(resId);
    }

    //左侧文字
    public void setLeftLayoutTextView(String title) {
        leftText.setText(title);
    }

    //右侧文字
    public void setRightLayoutTextView(String title) {
        rightText.setText(title);
    }

    //左侧点击事件
    public void setLeftLayoutClickListener(OnClickListener listener) {
        leftLayout.setOnClickListener(listener);
    }

    //右侧点击事件
    public void setRightLayoutClickListener(OnClickListener listener) {
        rightLayout.setOnClickListener(listener);
    }

    //右侧图片可见
    public void setRightLayoutImageVisibility(int visibility) {
        rightImage.setVisibility(visibility);
    }

    //获取右侧Text
    public TextView getRightText() {
        return rightText;
    }

    //获取右侧图片引用
    public ImageView getRightImage() {
        return rightImage;
    }

    //获取标题中间的引用
    public LinearLayout getCenterLayout() {
        return centerLayout;
    }

    //标题栏标题
    public void setTitle(String title) {
        titleView.setText(title);
    }

    public void setTitleSize(float size) {
        titleView.setTextSize(size);
    }

    public void setTxtNumberVisible(int visibility) {
        txtNumber.setVisibility(visibility);
    }

    public void setTxtNumberText(String text) {
        txtNumber.setText(text);
    }

    public void setBackgroundColor(int color) {
        titleLayout.setBackgroundColor(color);
    }

    public RelativeLayout getLeftLayout() {
        return leftLayout;
    }

    public LinearLayout getRightLayout() {
        return rightLayout;
    }


    private void translucent() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //获取到状态栏的高度
            int statusHeight = getStatusBarHeight();
            final int[] llTitleHight = new int[1];
            childRoot.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    llTitleHight[0] = childRoot.getMeasuredHeight();
                    LogUtil.d(TAG, "wubaojie>>>onGlobalLayout: " + llTitleHight[0]);
                    getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });
            //动态的设置隐藏布局的高度
            LayoutParams params = (LayoutParams) childRoot.getLayoutParams();
            params.topMargin = statusHeight + llTitleHight[0];
            childRoot.setLayoutParams(params);
        }
    }

    /**
     * 通过反射的方式获取状态栏高度
     *
     * @return
     */
    private int getStatusBarHeight() {
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = Integer.parseInt(field.get(obj).toString());
            return getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
